<template>
    <div class="app-container">
        <!-- 搜索工作栏 -->
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="域名" prop="domain">
                <el-input
                    v-model="queryParams.domain"
                    placeholder="请输入域名"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请选择状态"
                    clearable
                    @keyup.enter.native="handleQuery"
                >
                    <el-option :label="'正常'" :value="1"></el-option>
                    <el-option :label="'停用'" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="handleQuery"
                    >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <!-- 操作工具栏 -->
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="openForm({})"
                    >新增</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    plain
                    size="mini"
                    @click="patchDel"
                    type="danger"
                    icon="el-icon-delete"
                    >批量删除</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    plain
                    size="mini"
                    type="primary"
                    icon="el-icon-edit"
                    @click="showUpdateStatus"
                    >批量更新状态</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    :loading="exportLoading"
                    v-hasPermi="['tk:domain:export']"
                    >导出</el-button
                >
            </el-col>
        </el-row>

        <el-table
            v-loading="loading"
            :data="list"
            :stripe="true"
            :show-overflow-tooltip="true"
            border
            size="mini"
            max-height="680"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
                label="编号"
                prop="id"
                width="120"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                label="部门名称"
                prop="deptName"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                label="域名"
                prop="domain"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                label="已下发"
                prop="issued"
                align="center"
                show-overflow-tooltip
            >
               <template #default="scope">
                   {{ scope.row.issued ? scope.row.issued.join('，') : "" }}
               </template>
            </el-table-column>
            <el-table-column
                label="操作人"
                prop="nickname"
                align="center"
            ></el-table-column>
            <el-table-column
                label="创建日期"
                align="center"
                prop="createTime"
                width="150"
                show-overflow-tooltip
            >
                <template v-slot="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column
                label="状态"
                prop="status"
                align="center"
                show-overflow-tooltip
                width="200"
            >
                <template v-slot="scope">
                    <el-switch
                        v-model="scope.row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="handleStatusChange(scope.row)"
                    />
                </template>
            </el-table-column>

            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                width="200"
            >
                <template v-slot="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-thumb"
                        @click="openPublish(scope.row)"
                        v-hasPermi="['tk:domain:publish']"
                        >下发</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="openForm(scope.row)"
                        v-hasPermi="['tk:domain:update']"
                        >修改</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['tk:domain:delete']"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNo"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
        <domainForm ref="formRef" @success="getList" />
        <publishForm ref="pubRef" @success="getList" />
        <!-- ...........批量修改状态弹窗............ -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="300px">
            <div>
                <el-radio-group v-model="switchValue">
                    <el-radio :label="1">开启</el-radio>
                    <el-radio :label="0">关闭</el-radio>
                </el-radio-group>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateDomainStatus"
                    >确 定</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>
<script>
import {
    getDomainList,
    deleteDomain,
    exportDomain,
    batchDeleteDomain,
    updataDomainStatus,
} from "@/api/domain";
import domainForm from "./domainForm";
import publishForm from "./publishForm";
import { Loading } from "element-ui";
import router from "@/router/index";

export default {
    components: {
        domainForm,
        publishForm,
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 表格数据
            list: [],
            total: 0,
            ids: [],
            // 显示搜索条件
            showSearch: true,
            exportLoading: false,
            dialogVisible: false,
            switchValue: 1,
            queryParams: {
                pageNo: 1,
                pageSize: 10,
                domain: null,
                status: null,
            },
        };
    },
    methods: {
        async getList() {
            this.loading = true;
            const { code, data } = await getDomainList(this.queryParams);
            if (code === 0) {
                this.list = data.list;
                this.total = data.total;
            }

            this.loading = false;
        },
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNo = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.$refs["queryForm"].resetFields();
            this.handleQuery();
        },
        /** 添加/修改操作 */
        openForm(row) {
            this.$refs["formRef"].open(row);
        },
        /** 发布 */
        openPublish(row) {
            this.$refs["pubRef"].open(row);
        },

        /** 删除按钮操作 */
        async handleDelete(row) {
            const id = row.id;
            await this.$modal
                .confirm('是否确认删链接域名为"' + row.domain + '"的数据项?')
                .then(async () => {
                    try {
                        const res = await deleteDomain({
                            id: id,
                        });
                        if (res.code === 0) {
                            await this.getList();
                            this.$modal.msgSuccess("删除成功");
                        } else {
                            this.$modal.msgError(res.msg);
                        }
                    } catch {}
                }).catch(() => {});
        },
        /** 导出按钮操作 */
        async handleExport() {
            await this.$modal.confirm("是否确认导出所有域名数据项?");
            let downloadLoadingInstance;
            try {
                this.exportLoading = true;
                downloadLoadingInstance = Loading.service({
                    text: "正在导出数据，请稍候",
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.7)",
                });
                const res = await exportDomain(this.queryParams);
                let routerName = router.app._route.meta.title;
                let fileName = "域名-";
                if (routerName) {
                    fileName = routerName + "-";
                }
                this.$download.excel(
                    res,
                    fileName +
                        this.parseTime(new Date(), "{y}{m}{d}{h}{i}{s}") +
                        ".xls"
                );
            } finally {
                this.exportLoading = false;
                downloadLoadingInstance.close();
            }
        },
        hasSelectdIds() {
            if (this.ids.length === 0) {
                this.$message.info("请选择要操作的项");
            }
            return this.ids.length > 0;
        },
        async patchDelete() {
            const { code, msg } = await batchDeleteDomain({
                ids: this.ids.join(","),
            });
            if (code === 0) {
                this.$modal.msgSuccess("删除成功");
                this.getList();
            } else {
                this.$modal.msgError(msg);
            }
        },
        patchDel() {
            const hasIds = this.hasSelectdIds();
            if (hasIds) {
                this.$confirm("请确认是否删除选中域名?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.patchDelete();
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除",
                        });
                    });
            }
        },
        showUpdateStatus() {
            const hasIds = this.hasSelectdIds();
            if (hasIds) {
                this.dialogVisible = true;
            }
        },
        async updateDomainStatus() {
            const { code, msg } = await updataDomainStatus({
                ids: this.ids.join(","),
                status: this.switchValue,
            });
            if (code === 0) {
                this.$modal.msgSuccess("状态已更新");
                this.dialogVisible = false;
                this.switchValue = 1;
                this.getList();
            } else {
                this.$modal.msgError(msg);
            }
        },
        async handleStatusChange(row) {
            const res = await updataDomainStatus({
                status: row.status,
                ids: row.id,
            });
            if (res.code === 0) {
                this.$modal.msgSuccess("状态更新成功");
            }
        },
    },
    created() {
        this.getList();
    },
};
</script>
